/**
 * 
 */
package signGrammar.expressions.util;

import java.util.regex.Pattern;

public enum EExpressionElementClass {
	VARIABLE_NAME("([a-z])+[a-z0-9]*_[0-9]*"),
	NONTERMINAL_NAME("([A-Z])+_[0-9]*"),
	BINARY_EXPRESSION_SIGN("\\-|\\+|\\*|x|"),
	OPENING_BRAKED("\\("),
	CLOSING_BRAKED("\\)"),
	UNARY_EXPRESSION_SIGN("~");
	
	public static class CWrongExpressionElementClass extends RuntimeException{
		private static final long serialVersionUID = 1L;
		public CWrongExpressionElementClass() { }
		public CWrongExpressionElementClass(String message, Throwable cause) {
			super(message, cause);
		}
		public CWrongExpressionElementClass(String message) {
			super(message);
		}
		public CWrongExpressionElementClass(Throwable cause) {
			super(cause);
		}
	}
	
	private String pattern;
	private EExpressionElementClass(String ptr) {
		pattern = ptr;
	}
	
	public boolean match(String var){
		return Pattern.matches(pattern, var);
	}
	
	public static EExpressionElementClass getFor(String s){
		for( EExpressionElementClass e: values() )
			if( Pattern.matches(e.pattern, s) )
				return e;
		throw new CWrongExpressionElementClass("Wrong expression element class for: " + s);
	}
	
	public static String parseVarType(String var){
		return var.split("_")[0];
	}
	public static Integer parseVarID(String var){
		try{
			return Integer.parseInt( var.split("_")[1] );
		}catch(Exception e){
			return 0;
		}
	}
}